﻿package core.display {
	
	import base.BaseMc;
	import flash.events.MouseEvent;
	import core.net.socket.cmd.WindowMinCmd;
	import core.net.socket.cmd.WindowQuiteCmd;
	import core.net.socket.cmd.LogoutCmd;
	import core.events.CmdEvent;
	import core.events.MainMenuEvent;
	import core.display.LoginMenu;
	
	
	public class MainMenu extends BaseMc {
		
		private var popup:MenuPopup;
		private var btnMenu:BtnMenu;
		
		public function MainMenu() {
			
		}
		
		protected override function onInit():void {
			this.initPopup();
			this.initBtnMenu();
		}
		
		protected override function onRemove():void {
			this.btnMenu.removeEventListener(MouseEvent.CLICK,onBtnMenuClick);
		}
		
		private function initBtnMenu():void{
			this.btnMenu = new BtnMenu();
			this.btnMenu.x = 0;
			this.btnMenu.y = 0;
			this.btnMenu.addEventListener(MouseEvent.CLICK,onBtnMenuClick);
			this.addChild(this.btnMenu);
		}
		
		private function initPopup():void {
			this.popup = new MenuPopup();
			this.popup.x = 0;
			this.popup.y = -370;
			this.popup.visible = false;
			this.popup.btnMin.addEventListener(MouseEvent.CLICK,onBtnMinClick);
			this.popup.btnQuite.addEventListener(MouseEvent.CLICK,onBtnQuiteClick);
			this.popup.btnLogout.addEventListener(MouseEvent.CLICK,onLogoutClick);
			this.addChild(this.popup);
		}
		
		private function onBtnMenuClick(e:MouseEvent):void {
			this.popup.visible = !this.popup.visible;
		}
		
		private function onBtnMinClick(e:MouseEvent):void {
			this.sendCmd(WindowMinCmd.make());
			this.popup.visible = false;
		}
		
		private function onLogoutClick(e:MouseEvent):void {
			this.popup.visible = !this.popup.visible;
			this.dispatchEvent(new MainMenuEvent(MainMenuEvent.LOGOUT));
		}
		
		private function onBtnQuiteClick(e:MouseEvent):void {
			this.sendCmd(WindowQuiteCmd.make());
			this.popup.visible = false;
		}
		
		
		
	}
	
}
